<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Category extends MX_Controller {

	private $module = 'category';
	private $table = 'category';
	function __construct(){
		parent::__construct();
		$this->load->model($this->module.'_model','model');
		$this->load->model('admincp_modules/admincp_modules_model');
		if($this->uri->segment(1)=='admincp'){
			if($this->uri->segment(2)!='login'){
				if(!$this->session->userdata('userInfo')){
					header('Location: '.PATH_URL.'admincp/login');
					return false;
				}
				$get_module = $this->admincp_modules_model->check_modules($this->uri->segment(2));
				$this->session->set_userdata('ID_Module',$get_module[0]->id);
				$this->session->set_userdata('Name_Module',$get_module[0]->name);
			}
			$this->template->set_template('admin');
			$this->template->write('title','Admin Control Panel');
		}
	}
	/*------------------------------------ Admin Control Panel ------------------------------------*/
	public function admincp_index(){
		modules::run('admincp/chk_perm',$this->session->userdata('ID_Module'),'r',0);
		$default_func = 'created';
		$default_sort = 'DESC';
		$data = array(
			'module'=>$this->module,
			'module_name'=>$this->session->userdata('Name_Module'),
			'default_func'=>$default_func,
			'default_sort'=>$default_sort
		);
		$this->template->write_view('content','BACKEND/index',$data);
		$this->template->render();
	}


	public function admincp_update($id=0){
		if($id==0){
			modules::run('admincp/chk_perm',$this->session->userdata('ID_Module'),'w',0);
		}else{
			modules::run('admincp/chk_perm',$this->session->userdata('ID_Module'),'r',0);
		}
		$result[0] = array();
		$parent_id = '';
		if($id!=0){
			$result = $this->model->getDetailManagement($id);
			$parent_id = $result[0]->parent_id;
		}
		$data = array(
			'result'=>$result[0],
			'module'=>$this->module,
			'id'=>$id,
			'options' => $this->model->_getSelectList($parent_id),
		);
		$this->template->write_view('content','BACKEND/ajax_editContent',$data);
		$this->template->render();
	}

	public function admincp_save(){
		$perm = modules::run('admincp/chk_perm',$this->session->userdata('ID_Module'),'w',1);
		if($perm=='permission-denied'){
			print $perm;
			exit;
		}
		if($_POST){
			if($this->model->saveManagement()){
				print 'success';
				exit;
			}
		}
	}

	public function admincp_delete(){
		$perm = modules::run('admincp/chk_perm',$this->session->userdata('ID_Module'),'d',1);
		if($perm=='permission-denied'){
			print $perm;
			exit;
		}
		if($this->input->post('id')){
			$id = $this->input->post('id');
			$result = $this->model->getDetailManagement($id);
			modules::run('admincp/saveLog',$this->module,$id,'Delete','Delete');
			$this->db->where('id',$id);
			if($this->db->delete(PREFIX.$this->table)){
				//Xóa hình khi Delete
				@unlink(BASEFOLDER.DIR_UPLOAD_NEWS.$result[0]->image);
				return true;
			}
		}
	}

	public function admincp_ajaxLoadContent(){
		$this->load->library('AdminPagination');
		$config['total_rows'] = $this->model->getTotalsearchContent();
		$config['per_page'] = $this->input->post('per_page');
		$config['num_links'] = 3;
		$config['func_ajax'] = 'searchContent';
		$config['start'] = $this->input->post('start');
		$this->adminpagination->initialize($config);

		$result = $this->model->getsearchContent($config['per_page'],$this->input->post('start'));
		$data = array(
			'result'=>$result,
			'per_page'=>$this->input->post('per_page'),
			'start'=>$this->input->post('start'),
			'module'=>$this->module
		);
		$this->session->set_userdata('start',$this->input->post('start'));
		$this->load->view('BACKEND/ajax_loadContent',$data);
	}

	public function admincp_ajaxUpdateStatus(){
		$perm = modules::run('admincp/chk_perm',$this->session->userdata('ID_Module'),'w',1);
		if($perm=='permission-denied'){
			print '<script type="text/javascript">show_perm_denied()</script>';
			$status = $this->input->post('status');
			$data = array(
				'status'=>$status
			);
		}else{
			if($this->input->post('status')==0){
				$status = 1;
			}else{
				$status = 0;
			}
			$data = array(
				'status'=>$status
			);
			modules::run('admincp/saveLog',$this->module,$this->input->post('id'),'status','update',$this->input->post('status'),$status);
			$this->db->where('id', $this->input->post('id'));
			$this->db->update(PREFIX.$this->table, $data);
		}

		$update = array(
			'status'=>$status,
			'id'=>$this->input->post('id'),
			'module'=>$this->module
		);
		$this->load->view('BACKEND/ajax_updateStatus',$update);
	}
	/*------------------------------------ End Admin Control Panel --------------------------------*/


	/*------------------------------------ FRONTEND ------------------------------------*/
	public function list_category()
	{
		$data = array();

	}

	function Menu() {
		$Menu = $this->model->fetch('id,title_vi,title_en,title_gr,link,slug', PREFIX.'category', "`parent_id` = 0", 'order_view', 'asc');
		if (!empty($Menu)) {
			foreach ($Menu as $M) {
				$M->submenu = $this->model->fetch('title_vi,title_en,title_gr,link,slug', PREFIX.'category', "`parent_id` = '{$M->id}' AND `status` = 1",'order_view','asc');

				if($M->submenu == NULL){
					$M->submenu = $this->model->fetch('title_vi,title_en,title_gr,link,slug', PREFIX.'news', "`parent_id` = '{$M->id}' AND `status` = 1",'order_view','asc');
					//pr($M->submenu,1);
				}
			}

			$data['Menu'] = $Menu;
			echo $this->load->view('FRONTEND/menu', $data, true);
		}
	}

	function Sitemap() {
		$Menu = $this->model->fetch('id,title_vi,title_en,title_gr,link,slug', PREFIX.'category', "`parent_id` = 0", 'order_view', 'asc');
		if (!empty($Menu)) {
			foreach ($Menu as $M) {
				$M->submenu = $this->model->fetch('title_vi,title_en,title_gr,link,slug', PREFIX.'category', "`parent_id` = '{$M->id}' AND `status` = 1",'order_view','asc');

				if($M->submenu == NULL){
					$M->submenu = $this->model->fetch('title_vi,title_en,title_gr,link,slug', PREFIX.'news', "`parent_id` = '{$M->id}' AND `status` = 1",'order_view','asc');
					//pr($M->submenu,1);
				}
			}

			$data['Menu'] = $Menu;
			
			$this->template->write('title', 'Website map | Bang Huu Law Firm');
			$this->template->write_view('content', 'FRONTEND/sitemap', $data);
			$this->template->render();
		}
		else{
			redirect(PATH_URL);
		}

	}

	function munu_left($segment=''){
		$data = array();
		if($segment == 'users'){
			$data['users'] = true;
		}
		else if($segment != ''){
			$this->load->model('category_model','category_model');
			$cate = $this->category_model->getSingleNode($segment);

			if (!empty($cate)) {
				$submenu = $this->model->fetch('title_vi,title_en,title_gr,link,slug', PREFIX.'category', "`parent_id` = '{$cate->id}' AND `status` = 1",'order_view','asc');

				if($submenu == NULL){
					$submenu = $this->model->fetch('title_vi,title_en,title_gr,link,slug', PREFIX.'news', "`parent_id` = '{$cate->id}' AND `status` = 1",'order_view','asc');
				}
				$data['submenu'] = $submenu;

			}
		}
		echo $this->load->view('FRONTEND/menu_left', $data, true);
	}
	/*------------------------------------ End FRONTEND --------------------------------*/
}
